package com.mlj.service;

import com.mlj.dao.UserMapper;
import com.mlj.pojo.Temp;
import com.mlj.pojo.User2;
import com.mlj.util.Md5Util;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public User2 selectUser(String userName) {
        return userMapper.selectUser(userName);
    }

    @Override
    public Result changePassword(Temp temp, HttpSession session){
        Result result = new Result();
        User2 user = (User2)session.getAttribute("User");
        String psw=user.getUser_password();
        if(psw.equals(Md5Util.secretPass(user.getUser_name(),temp.getPassword()))){
            //正确，对比新密码和确认新密码是否一致
            System.out.println("=========="+1);
            if(temp.getNpassword().equals(temp.getRpassword())) {
                //调用修改密码方法
                System.out.println("=========="+2);
                String pw =Md5Util.secretPass(user.getUser_name(),temp.getRpassword());
                int n = userMapper.updatePassword(pw,user.getUser_id());
                System.out.println("=========="+n);
                if(n>0) {
                    result.setStatus(200);
                    result.setMessage("修改成功，请重新登录");
                }else {
                    result.setStatus(500);
                    result.setMessage("修改密码失败");
                }
            }else {
                result.setStatus(500);
                result.setMessage("两次新密码输入不一致");
            }
        }else{
            result.setStatus(500);
            result.setMessage("原始密码输入错误");
        }
        return result;
    }
}
